﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.SceneControl;


namespace PloobsEngine.IA
{
    public class PathFollowAgent : IAgent
    {
        private IObject obj;
        private IPathFinder pf;
        private IMap map;
        private LinkedList<Waypoint> wpts;
        private int step = 0;
        private FollowPathActuator fp;
        Waypoint w1;
        Waypoint w2;
        Waypoint w3;
        Waypoint w4;
        Waypoint[] w;

        public PathFollowAgent(IObject obj, IMap map)
        {
            this.obj = obj;
            this.obj.Agente = this;
            pf = new AStar(map);
            this.map = map;
            wpts = pf.GetPath(AIWorkType.SINGLE_TIME, map.Waypoints.GetWaypointsList()[0], map.Waypoints.GetWaypointsList()[map.Waypoints.GetWaypointsList().Count -1]);
            fp = new FollowPathActuator(obj);

            //w1 = new Waypoint();
            //w1.WorldPos = obj.Position;

            //w2 = new Waypoint();
            //w2.WorldPos = new Vector3(150,w1.WorldPos.Y,90);

            //w3 = new Waypoint();
            //w3.WorldPos = new Vector3(-10, w1.WorldPos.Y, -100);

            //w4 = new Waypoint();
            //w4.WorldPos = new Vector3(-10, w1.WorldPos.Y, 100);

            //w = new Waypoint[] { w2, w3, w4 };

            w1 = wpts.First<Waypoint>();
            wpts.RemoveFirst();
            w2 = wpts.First<Waypoint>();
            wpts.RemoveFirst();
            fp.SetPath(w1.WorldPos,w2.WorldPos );
        }

        #region IAgent Members

        int a = 0;
        public void Update(Microsoft.Xna.Framework.GameTime gt)
        {
            ActuatorSensorInformation  resp = fp.PerformAct();
            if(resp.Ended == true)
            {
                w1 = wpts.First<Waypoint>();
                wpts.RemoveFirst();
                fp.SetPath(w2.WorldPos,w1.WorldPos);
                w2 = w1;
            }
        }

        public IObject Obj
        {
            get
            {
                return obj;
            }
            set
            {
                this.obj = value;
            }
        }

        #endregion
    }
}
